clear all
set more off
	
global dir = "C:\Users\mrueda\Documents\Emory\Papers\Networks_persistance\do_files\do_files_APSA21\post_JOP\Replication_BJPS\"	

cd "$dir"

use "Data\cand_level_persist_rep.dta",clear	

gen treat=0
		replace treat=1 if margin_victory>0&margin_victory~=.
		replace treat=. if margin_victory==.

		gen treat_margin_victory=treat*margin_victory

	texdoc close 
	cap erase "$dir/Tables/Table3.tex"
	texdoc init "$dir/Tables/Table3.tex", force
	
	tex \begin{table}[tbph]
	tex \caption{Effect of donating to an election winner on future donations (candidate's family members vs. Non members)}\label{tab:donation_fam_nofam}
	tex \centering
	tex \begin{tabular}{l c c H} \hline
	tex Outcome : & Any race & Mayor & b2 \\ 
	tex & (1) & (2) & (3) \\ \hline
	tex \multicolumn{2}{l}{Panel A: Candidates' family members}&\\
	tex & & & \\
	
	*Model 1
	foreach x in donate_15any b5{
	
		

		

		
		*Family
		*Regressions
		*Summary statistics for the mean
		quietly: regress f`x' treat treat_margin_victory margin_victory , vce(cluster muni_code)
		quietly sum f`x' if e(sample)
			local fmean_`x' : di %5.3f r(mean)
			local sd_`x' : di %5.3f r(sd) 
			
		rdrobust f`x' margin_victory ,  vce(cluster muni_code)

		*Local's for the table
		local fbw_`x' : di %5.2f `e(h_l)'
		local fNeff_`x' = `e(N_h_l)'+`e(N_h_r)'
		local fN_`x' = `e(N)'
		local fbeta1_`x' : di %5.3f `e(tau_cl)'
		local fbeta2_`x' : di %5.3f `e(tau_bc)'

		*Confidence intervals
			local fser1_`x' : di %5.3f `e(ci_l_rb)'
			local fser2_`x' : di %5.3f `e(ci_r_rb)'
			
/* HERE*/	local fem1_`x' = (`fbeta1_`x''/`fmean_`x'')*100 
			local fem1_`x' : di %5.2f `fem1_`x''
			
		*P-values
		local fpval2_`x' : di %5.3f `e(pv_rb)'
		scalar fpval2_`x' = e(pv_rb)
		
		
		regress f`x' treat treat_margin_victory margin_victory , vce(cluster muni_code)

		local fN_`x' : di %5.0f e(N)
		local fR2_`x' : di %5.3f e(r2)

		matrix b = e(b)
		matrix v = e(V)
		matrix res=r(table)
		
		local fb1_`x' : di %5.3f b[1,1]
		local fse1_`x' : di %5.3f sqrt(v[1,1])
		local fp_v_`x' :di %5.3f res[4,1]
		local fuci_`x': di %5.3f res[6,1]
		local flci_`x': di %5.3f res[5,1]



		
		*No Family
		*Regressions
		*Summary statistics for the mean
		quietly: regress nf`x' treat treat_margin_victory margin_victory , vce(cluster muni_code)
		quietly sum nf`x' if e(sample)
			local nfmean_`x' : di %5.3f r(mean)
			local sd_`x' : di %5.3f r(sd) 

		rdrobust nf`x' margin_victory ,  vce(cluster muni_code)

		*Local's for the table
		local nfbw_`x' : di %5.2f `e(h_l)'
		local nfNeff_`x' = `e(N_h_l)'+`e(N_h_r)'
		local nfN_`x' = `e(N)'
		local nfbeta1_`x' : di %5.3f `e(tau_cl)'
		local nfbeta2_`x' : di %5.3f `e(tau_bc)'

		*Confidence intervals
			local nfser1_`x' : di %5.3f `e(ci_l_rb)'
			local nfser2_`x' : di %5.3f `e(ci_r_rb)'
			
/* HERE*/	local nfem1_`x' = (`nfbeta1_`x''/`nfmean_`x'')*100 
			local nfem1_`x' : di %5.2f `nfem1_`x''
			
		*P-values
		local nfpval2_`x' : di %5.3f `e(pv_rb)'
		scalar nfpval2_`x' = e(pv_rb)
		
		regress nf`x' treat treat_margin_victory margin_victory , vce(cluster muni_code)

		local nfN_`x' : di %5.0f e(N)
		local nfR2_`x' : di %5.3f e(r2)

		matrix b = e(b)
		matrix v = e(V)
		matrix res=r(table)
		
		local nfb1_`x' : di %5.3f b[1,1]
		local nfse1_`x' : di %5.3f sqrt(v[1,1])
		local nfp_v_`x' :di %5.3f res[4,1]
		local nfuci_`x': di %5.3f res[6,1]
		local nflci_`x': di %5.3f res[5,1]

	}
	


		

	*Continue table
	tex \multicolumn{2}{l}{Local linear}\\
	tex Electoral victory & `fbeta1_donate_15any' & `fbeta1_b5' & `fbeta1_b3' \\
	tex \ \ \ \ Robust p-value & `fpval2_donate_15any' & `fpval2_b5' & `fpval2_b3' \\
	tex \ \ \ \ CI 95\%  & [`fser1_donate_15any',`fser2_donate_15any'] & [`fser1_b5',`fser2_b5'] & [`fser1_b3',`fser2_b3'] \\
	tex & & & \\
	
	tex \multicolumn{2}{l}{Parametric (linear)}\\
	tex Electoral victory & `fb1_donate_15any' & `fb1_b5'  \\
	tex \ \ \ \ Robust p-value & `fp_v_donate_15any' & `fp_v_b5' \\
	tex \ \ \ \ CI 95\%  & [`flci_donate_15any',`fuci_donate_15any'] & [`flci_b5',`fuci_b5']  \\
	tex & & & \\
	
	tex Observations & `fN_donate_15any' & `fN_b5' & `fN_b3' \\
	tex Bandwidth obs. & `fNeff_donate_15any' & `fNeff_b5' & `fNeff_b3' \\
	tex Mean & `fmean_donate_15any' & `fmean_b5' & `fmean_b3' \\
	tex Bandwidth & `fbw_donate_15any' & `fbw_b5' & `fbw_b3' \\ 
	
	tex & & & \\ \hline
	tex {Panel B: Non-family members}&  \\  
	tex & & & \\

	tex \multicolumn{2}{l}{Local linear}\\
	tex Electoral victory & `nfbeta1_donate_15any' & `nfbeta1_b5' & `nfbeta1_b3' \\
	tex \ \ \ \ Robust p-value & `nfpval2_donate_15any' & `nfpval2_b5' & `nfpval2_b3' \\
	tex \ \ \ \ CI 95\%  & [`nfser1_donate_15any',`nfser2_donate_15any'] & [`nfser1_b5',`nfser2_b5'] & [`nfser1_b3',`nfser2_b3'] \\
	tex & & & \\
	
	tex \multicolumn{2}{l}{Parametric (linear)}\\
	tex Electoral victory & `nfb1_donate_15any' & `nfb1_b5'  \\
	tex \ \ \ \ Robust p-value & `nfp_v_donate_15any' & `nfp_v_b5' \\
	tex \ \ \ \ CI 95\%  & [`nflci_donate_15any',`nfuci_donate_15any'] & [`nflci_b5',`nfuci_b5']  \\
	tex & & & \\
	
	tex Observations & `nfN_donate_15any' & `nfN_b5' & `nfN_b3' \\
	tex Bandwidth obs. & `nfNeff_donate_15any' & `nfNeff_b5' & `nfNeff_b3' \\
	tex Mean & `nfmean_donate_15any' & `nfmean_b5' & `nfmean_b3' \\
	tex Bandwidth & `nfbw_donate_15any' & `nfbw_b5' & `nfbw_b3' \\ \hline
	tex \end{tabular}
	tex \parbox{160mm}{ \footnotesize{Local linear estimates of average treatment effects at the cutoff estimated with triangular kernel weights and optimal MSE bandwidth. Robust p-values with clustering at the municipality level and 95\% robust confidence intervals are computed following \cite{calonico_robust_2014}. Parametric linear model specification includes interaction of the treatment with the running variable and running variable. Bandwidth obs. denotes the number of observations in the optimal MSE bandwidth.
	tex }
	tex }
	tex \end{table}
	cap texdoc close 
	
	
